/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CardsCache.java
 *
 * Created on Dec 3, 2011, 9:27:19 PM
 */
package makao;

import javax.swing.JSlider;
import shared.Card;

/**
 *
 * @author gekon
 */
public class CardsCache extends javax.swing.JFrame
{

    private MakaoView makaoView = null;

    /** Creates new form CardsCache */
    public CardsCache(MakaoView makaoView)
    {
        this.makaoView = makaoView;
        initComponents();
        cardsCachePanel.setElements(jScrollPane1, jSliderPosition);
        jSliderPosition.setMinimum(0);
        jSliderPosition.setMaximum(0);
    }

    public void demandType(int type)
    {
        makaoView.getPlayerAction().throwCardsDemandType(cardsCachePanel.getFinalCards(), type);
        makaoView.getCardCacheFrame().setEnabled(true);
        makaoView.getCardsPanel().setEnabled(true);
        makaoView.getTablePanel().setEnabled(true);
        cardsCachePanel.setNewCall(true);
        this.setVisible(false);
    }

    public void demandSuit(int suit)
    {
        makaoView.getPlayerAction().throwCardsDemandSuit(cardsCachePanel.getFinalCards(), suit);
        makaoView.getCardCacheFrame().setEnabled(true);
        makaoView.getCardsPanel().setEnabled(true);
        makaoView.getTablePanel().setEnabled(true);
        cardsCachePanel.setNewCall(true);
        this.setVisible(false);
    }

    public CardsCachePanel getCardsCachePanel()
    {
        return cardsCachePanel;
    }


    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        cardsCachePanel = new makao.CardsCachePanel();
        jButtonCancelAll = new javax.swing.JButton();
        jButtonCancelOne = new javax.swing.JButton();
        jButtonConfirm = new javax.swing.JButton();
        jSliderPosition = new javax.swing.JSlider();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(makao.MakaoApp.class).getContext().getResourceMap(CardsCache.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setAlwaysOnTop(true);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setForeground(resourceMap.getColor("Form.foreground")); // NOI18N
        setName("Form"); // NOI18N
        setResizable(false);

        jScrollPane1.setBackground(resourceMap.getColor("jScrollPane1.background")); // NOI18N
        jScrollPane1.setBorder(null);
        jScrollPane1.setVerticalScrollBarPolicy(javax.swing.ScrollPaneConstants.VERTICAL_SCROLLBAR_NEVER);
        jScrollPane1.setName("jScrollPane1"); // NOI18N

        cardsCachePanel.setBackground(resourceMap.getColor("cardsCachePanel.background")); // NOI18N
        cardsCachePanel.setName("cardsCachePanel"); // NOI18N
        cardsCachePanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                cardsCachePanelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout cardsCachePanelLayout = new javax.swing.GroupLayout(cardsCachePanel);
        cardsCachePanel.setLayout(cardsCachePanelLayout);
        cardsCachePanelLayout.setHorizontalGroup(
            cardsCachePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 669, Short.MAX_VALUE)
        );
        cardsCachePanelLayout.setVerticalGroup(
            cardsCachePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 150, Short.MAX_VALUE)
        );

        jScrollPane1.setViewportView(cardsCachePanel);

        jButtonCancelAll.setText(resourceMap.getString("jButtonCancelAll.text")); // NOI18N
        jButtonCancelAll.setToolTipText(resourceMap.getString("jButtonCancelAll.toolTipText")); // NOI18N
        jButtonCancelAll.setName("jButtonCancelAll"); // NOI18N
        jButtonCancelAll.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelAllActionPerformed(evt);
            }
        });

        jButtonCancelOne.setText(resourceMap.getString("jButtonCancelOne.text")); // NOI18N
        jButtonCancelOne.setName("jButtonCancelOne"); // NOI18N
        jButtonCancelOne.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelOneActionPerformed(evt);
            }
        });

        jButtonConfirm.setText(resourceMap.getString("jButtonConfirm.text")); // NOI18N
        jButtonConfirm.setName("jButtonConfirm"); // NOI18N
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        jSliderPosition.setForeground(resourceMap.getColor("jSliderPosition.foreground")); // NOI18N
        jSliderPosition.setSnapToTicks(true);
        jSliderPosition.setEnabled(false);
        jSliderPosition.setName("jSliderPosition"); // NOI18N
        jSliderPosition.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                jSliderPositionStateChanged(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 669, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonCancelAll)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancelOne)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSliderPosition, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(6, 6, 6)
                .addComponent(jButtonConfirm)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButtonCancelAll)
                        .addComponent(jButtonCancelOne))
                    .addComponent(jSliderPosition, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonConfirm))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cardsCachePanelMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_cardsCachePanelMouseClicked
    {//GEN-HEADEREND:event_cardsCachePanelMouseClicked
        // TODO add your handling code here:
        cardsCachePanel.selectCard(evt.getX(), evt.getY());
    }//GEN-LAST:event_cardsCachePanelMouseClicked

    private void jSliderPositionStateChanged(javax.swing.event.ChangeEvent evt)//GEN-FIRST:event_jSliderPositionStateChanged
    {//GEN-HEADEREND:event_jSliderPositionStateChanged
        // TODO add your handling code here:
        if (jSliderPosition.isEnabled() && ((JSlider) evt.getSource()).getValueIsAdjusting())
        {
            cardsCachePanel.changeOrder(jSliderPosition.getValue());
        }
    }//GEN-LAST:event_jSliderPositionStateChanged

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonConfirmActionPerformed
    {//GEN-HEADEREND:event_jButtonConfirmActionPerformed
        // TODO add your handling code here:
        if (!cardsCachePanel.getFinalCards().isEmpty())
        {

            switch (cardsCachePanel.getFinalCards().get(cardsCachePanel.getFinalCards().size() - 1).getType())
            {
                case Card.JACK:
                {
                    makaoView.getTypeFrame().setLocation(makaoView.getFrame().getLocationOnScreen().x + (makaoView.getFrame().getWidth() / 2 - 145), 
                                                        makaoView.getCardsPanel().getLocationOnScreen().y - 280);
                    //makaoView.getCardsPanel()
                    makaoView.getCardCacheFrame().setEnabled(false);
                    makaoView.getCardsPanel().setEnabled(false);
                    makaoView.getTypeFrame().setVisible(true);
                }
                break;
                case Card.ACE:
                {
                    makaoView.getSuitFrame().setLocation(makaoView.getFrame().getLocationOnScreen().x + (makaoView.getFrame().getWidth() / 2 - 180), 
                                                        makaoView.getCardsPanel().getLocationOnScreen().y - 220);
                    makaoView.getCardCacheFrame().setEnabled(false);
                    makaoView.getCardsPanel().setEnabled(false);
                    makaoView.getSuitFrame().setVisible(true);
                }
                break;
                default:
                {
                    if (cardsCachePanel.getFinalCards().get(cardsCachePanel.getFinalCards().size() - 1).getAsJoker() != null)
                    {
                        switch (cardsCachePanel.getFinalCards().get(cardsCachePanel.getFinalCards().size() - 1).getAsJoker().getType())
                        {
                            case Card.JACK:
                            {
                                makaoView.getTypeFrame().setLocation(makaoView.getFrame().getLocationOnScreen().x + (makaoView.getFrame().getWidth() / 2 - 145), 
                                                        makaoView.getCardsPanel().getLocationOnScreen().y - 280);
                                makaoView.getCardCacheFrame().setEnabled(false);
                                makaoView.getCardsPanel().setEnabled(false);
                                makaoView.getTypeFrame().setVisible(true);
                            }
                            break;
                            case Card.ACE:
                            {
                                makaoView.getSuitFrame().setLocation(makaoView.getFrame().getLocationOnScreen().x + (makaoView.getFrame().getWidth() / 2 - 180), 
                                                        makaoView.getCardsPanel().getLocationOnScreen().y - 220);
                                makaoView.getCardCacheFrame().setEnabled(false);
                                makaoView.getCardsPanel().setEnabled(false);
                                makaoView.getSuitFrame().setVisible(true);
                            }
                            break;
                            default:
                            {
                                makaoView.getPlayerAction().throwCards(cardsCachePanel.getFinalCards());
                                makaoView.getTablePanel().setEnabled(true);
                                cardsCachePanel.setNewCall(true);
                                this.setVisible(false);
                            }
                        }
                    } else
                    {
                        makaoView.getPlayerAction().throwCards(cardsCachePanel.getFinalCards());
                        makaoView.getTablePanel().setEnabled(true);
                        cardsCachePanel.setNewCall(true);
                        this.setVisible(false);
                    }
                }
            }
        } else
        {
            cardsCachePanel.setNewCall(true);
            this.setVisible(false);
        }
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    private void jButtonCancelOneActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonCancelOneActionPerformed
    {//GEN-HEADEREND:event_jButtonCancelOneActionPerformed
        // TODO add your handling code here:
        makaoView.getCardsPanel().undeleteCards(cardsCachePanel.cancelSelect());
        if (cardsCachePanel.getFinalCards().isEmpty())
        {
            makaoView.getTablePanel().setEnabled(true);
            cardsCachePanel.setNewCall(true);
            makaoView.getPlayerData().sortMyCards();
            this.setVisible(false);
        }
    }//GEN-LAST:event_jButtonCancelOneActionPerformed

    private void jButtonCancelAllActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButtonCancelAllActionPerformed
    {//GEN-HEADEREND:event_jButtonCancelAllActionPerformed
        // TODO add your handling code here:
        makaoView.getCardsPanel().undeleteCards(cardsCachePanel.cancelAll());
        makaoView.getTablePanel().setEnabled(true);
        cardsCachePanel.setNewCall(true);
        makaoView.getPlayerData().sortMyCards();
        this.setVisible(false);
    }//GEN-LAST:event_jButtonCancelAllActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[])
    {

        java.awt.EventQueue.invokeLater(new Runnable()
        {

            public void run()
            {
                //new CardsCache().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private makao.CardsCachePanel cardsCachePanel;
    private javax.swing.JButton jButtonCancelAll;
    private javax.swing.JButton jButtonCancelOne;
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSlider jSliderPosition;
    // End of variables declaration//GEN-END:variables
}
